﻿using MaterialDesignThemes.Wpf;
using MyFirstWPF.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyFirstWPF.View
{
    /// <summary>
    /// UserControl3.xaml 的交互逻辑
    /// </summary>
    public partial class UserControl3 : UserControl
    {
        public static string dbPath = "../../studyPlan.db";
        private DiaryInfo diary;
        public UserControl3()
        {
            InitializeComponent();
        }

        //引入数据库中的数据
        public UserControl3(DiaryInfo diary)
        {
            InitializeComponent();
            this.diary = diary;
            HistoryTitle.Text = diary.DiaryTitle;
            HistroyTime.Text = diary.BuildTime;

        }

        //显示数据
        private void See_Click(object sender, RoutedEventArgs e)
        {
            MyTitle.Text = diary.DiaryTitle;
            MyDiary.Text = diary.Diary;
        }

        //关闭按钮
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            //判断日志内容是否改动
            if (diary.DiaryTitle == MyTitle.Text && diary.Diary == MyDiary.Text)
            {
                DialogHost.CloseDialogCommand.Execute(null, null);
            }
            else
            {
                //判断是否进行修改
                StackPanel st = new StackPanel();
                st.Margin = new Thickness(12);
                TextBlock tb = new TextBlock();
                tb.FontSize = 20;
                tb.Text = "你确定要保存此次修改吗？";
                st.Children.Add(tb);
                Button bt1 = new Button();
                bt1.Margin = new Thickness(8);
                bt1.Content = "确定";
                bt1.Click += (oo, ee) =>
                {
                    //将修改的内容在数据库内更新
                    using (var db = new StudyPlanDb(dbPath))
                    {
                        diary.DiaryTitle = MyTitle.Text;
                        diary.Diary = MyDiary.Text;
                        db.Update(diary);
                    }
                    HistoryTitle.Text = diary.DiaryTitle;
                    DialogHost.CloseDialogCommand.Execute(null, null);
                };
                st.Children.Add(bt1);
                Button bt2 = new Button();
                bt2.Margin = new Thickness(8);
                bt2.Content = "取消";
                bt2.Click += (oo, ee) =>
                {
                    DialogHost.CloseDialogCommand.Execute(null, null);
                };
                st.Children.Add(bt2);
                DialogHost.OpenDialogCommand.Execute(st, null);
                return;
            }
        }

        //删除按钮
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            //确认用户是否删除该日志
            StackPanel st = new StackPanel();
            st.Margin = new Thickness(12);
            TextBlock tb = new TextBlock();
            tb.FontSize = 20;
            tb.Text = "你确定要删除这篇日志吗？";
            st.Children.Add(tb);
            Button bt1 = new Button();
            bt1.Margin = new Thickness(8);
            bt1.Content = "确定";
            bt1.Click += (oo, ee) =>
            {
                //将日志从数据库中删除
                using (var db = new StudyPlanDb(dbPath))
                {
                    db.Diarys.Where(x => x.Id == diary.Id).Delete();
                }
                WrapPanel p = (WrapPanel)Parent;
                p.Children.Remove(this);
                DialogHost.CloseDialogCommand.Execute(null, null);
            };
            st.Children.Add(bt1);
            Button bt2 = new Button();
            bt2.Margin = new Thickness(8);
            bt2.Content = "取消";
            bt2.Click += (oo, ee) =>
            {
                DialogHost.CloseDialogCommand.Execute(null, null);
            };
            st.Children.Add(bt2);
            DialogHost.OpenDialogCommand.Execute(st, null);
            return;
        }
    }
}
